<?php
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['PAD']."Aplications/ReportModels/report.vars.php");
form_pr('getResults','','','post');
MYDCONN();
	$TOTAL = 0;
$GLOBALS['MYT'] = $GLOBALS['MYD'];
	if(!isset($_POST['RsDate'])) { $_POST['RsDate'] = date("Y-m-01"); }
	if(!isset($_POST['RsProfit'])) { $_POST['RsProfit'] = 0; }
	$GET = $GLOBALS['MYD']->db_array("SELECT * FROM systemoffice.main_offices
			INNER JOIN officeschema.origenes ON OriNumero = office_number
			INNER JOIN officeschema.dominios ON dom_ID = OriDominio
			LEFT OUTER JOIN finantial_results ON RsOri = office_number AND (RsOffice IS NULL OR RsOffice = ref) AND
			MONTH(RsDate) = MONTH('".$_POST['RsDate']."') AND YEAR(RsDate) = YEAR('".$_POST['RsDate']."') AND RsType = 3 AND RsProfit = {$_POST['RsProfit']}
			WHERE dom_Activo = 1 AND OriTipo IN (2,1) AND OriNumero > 800
				AND OriFechaAlta < '".$_POST['RsDate']."'
				AND (OriFechaBaja > '".$_POST['RsDate']."' OR OriFechaBaja IS NULL)
			ORDER BY OriNumero");
/*	$GETMIN = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas 
		WHERE YEAR(FraFecha) = YEAR('{$_POST['RsDate']}') AND MONTH(FraFecha) = MONTH('{$_POST['RsDate']}') ORDER BY FraID ASC LIMIT 0,1");
	$GETMAX = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas 
		WHERE YEAR(FraFecha) = YEAR('{$_POST['RsDate']}') AND MONTH(FraFecha) = MONTH('{$_POST['RsDate']}') ORDER BY FraID DESC LIMIT 0,1");
	
			
	$GETBASE = $GLOBALS['MYT']->db_array("SELECT
	FraOrigen0,
	SUM(FraEnviado+FraProm) AS OUTAmount,
	SUM((FraARecibir*CorrRate)/Of_Rate2) AS FinResult
	FROM ordenespago
	INNER JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	INNER JOIN facturas ON FraOrdenPago = Op_Orden
	WHERE FraID BETWEEN {$GETMIN[1]['QR']} AND {$GETMAX[1]['QR']} AND (MONTH(FraFecha) = MONTH('{$_POST['RsDate']}') AND YEAR(FraFecha) = YEAR('{$_POST['RsDate']}')
		AND FraStatus IN (7,8,9,10,11,21)
	AND Of_Rate2 > 0
	GROUP BY OrderOffice
	ORDER BY OrderOffice");
	$TOTAL = 0;*/
	
	
 ?>
<div class="title"> % por Loja no periodo
	<?php
	mn_field('date','ED','RsDate',$_POST['RsDate'],'getResults','8','','');
	mn_field('button','ED','Consult','Consultar','','','','" style="padding:4px;margin:2px" onclick="
				document.getResults.action=\'Modules/FTK9D4D0/msetprofit.php\';
				subform(\'getResults\',\'app\');');

	mn_field('button','ED','Consult','Validar','','','','" style="padding:4px;margin:2px" onclick="
				document.getResults.action=\'_response/rsproxy.php?pr=cdc_valvalues\';
				subform(\'getResults\',\'RsResponse\');'); ?>
	<div id="RsResponse"></div>
</div>
<table width="100%">
<tr style="cell-padding:5px;background-color:#F2F2F2"><td>Numero</td><td>Loja</td><td>% Mensal</td></tr>
<?php
 for($i=1;$i<=$GET[0];$i++) {
 /*
	$ProfitAmount = $GETBASE[$i]['OUTAmount']-$GETBASE[$i]['FinResult'];
	if(FieldResult($GET,$h,'OUTAmount')>0){$Profit = ($ProfitAmount*100)/FieldResult($GET,$h,'OUTAmount');}else{$Profit = 0;}
	
 */
 
	$TOTAL += $GET[$i]['RsAmount'];
 	if($GET[$i]['RsAmount'] == ''){$GET[$i]['RsAmount']=0;}
 echo '<tr height="22px" bgcolor="'.gridlines($i).'"><td>';
	mn_field('hidden','ED','RsID'.$i,$GET[$i]['RsID'],'','','','');
	mn_field('hidden','ED','RsOffice'.$i,$GET[$i]['ref'],'','','','');
	mn_field('text','ED','OriNumero'.$i,$GET[$i]['OriNumero'],5,5,'','" readonly="'); echo '</td>
	<td>'.$GET[$i]['OriNombre'].'</td>
	<td>';
	mn_field('text','ED','RsAmount'.$i,$GET[$i]['RsAmount'],8,8,'','" style="text-align:right'); echo ' %</td>
    </tr>';
 } 
 echo '<tr height="22px" bgcolor="'.gridlines($i).'"><td></td>
	<td>Total: </td>
	<td>'.number_format($TOTAL,2,'.',',').' %</td>
    </tr>';
	mn_field('hidden','ED','RsCount',$GET[0],'','','','');
	mn_field('hidden','ED','RsType',3,'','','','');
	mn_field('hidden','ED','RsSetProfit',$_POST['RsProfit'],'','','',''); ?>
</table>
</form>